import  fs from     'fs';
import  path from   'path';

const __dirname = path.resolve();


const dir = path.join(__dirname, 'src');
const output = path.join(__dirname, 'spe.ts');
const files = ['Tween.ts','util.ts','Particle.ts','Emitter.ts','ForceField.ts','Constaint.ts','ParticleSystem.ts'];
const thirdPartyImports = new Set();
let codeBlocks = [];

for (const file of files) {
  let code = fs.readFileSync(path.join(dir, file), 'utf8');
  // 匹配 import 语句
  code = code.replace(/import\s+.*?from\s+['"]([^'"]+)['"];?/g, (match, mod) => {
    if (mod.startsWith('.') || mod.startsWith('/')) {
      // 本地 import，去掉
      return '';
    } else {
      // 第三方 import，收集
      thirdPartyImports.add(match);
      return '';
    }
  });
  // 去掉 export 语句
//   code = code.replace(/export\s+(default\s+)?/g, '');
  codeBlocks.push(`// ---- ${file} ----\n${code}`);
}

const outputContent = Array.from(thirdPartyImports).join('\n') + '\n\n' + codeBlocks.join('\n\n');
fs.writeFileSync(output, outputContent);
console.log('合并完成:', output);